package com.mo.common.lucene.utils;

import java.io.IOException;
import java.nio.file.Paths;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.wltea.analyzer.lucene.IKAnalyzer;

/**
 * created by harry on 2016/5/18.
 */
public class LuceneClient {
    public static Directory directory;
    /**
     * 写入操作
     * @return
     * @throws IOException
     */
    public IndexWriter getIndexWriter() throws IOException{
    	Analyzer analyzer = new IKAnalyzer();//中文分词
        //创建索引写入配置
        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);

        //创建索引写入对象
        IndexWriter indexWriter = new IndexWriter(this.directory, indexWriterConfig);
        return indexWriter;
    }
    /**
     * 读
     * @return
     * @throws IOException
     */
    public IndexReader getIndexReader() throws IOException{
    	IndexReader indexReader = DirectoryReader.open(this.directory);
        
        return indexReader;
    }
    public LuceneClient(String indexPath){
    	try {
			directory = FSDirectory.open(Paths.get(indexPath+"/"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
   /* public void setShardedJedisPool(String indexPath) throws IOException {
    	directory = FSDirectory.open(Paths.get(indexPath+"/"));
    }*/
}
